package TryCode.TCP;


import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    private String host = "127.0.0.1";
    private int port = 9999;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public Boolean is_closed = false;
    private String name;
    private Socket socket;
    private String message;

    public Client(String name) {
        this.name = name;
    }

    public Client(int port, String name) {
        this.port = port;
        this.name = name;
    }

    public Client(String host, int port, String name) {
        this.host = host;
        this.port = port;
        this.name = name;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    // 连接客户端
    public void connect(){
        try{
            this.socket = new Socket(this.host, this.port);
            OutputStream out = this.socket.getOutputStream();
            Message message = new Message(this.name, this.name, "run");
            out.write(message.toString().getBytes());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void close(){
        try{
            OutputStream out = this.socket.getOutputStream();
            Message message = new Message(this.name, this.name, "exit");
            out.write(message.toString().getBytes());

            synchronized (this.is_closed){
                this.is_closed = true;
            }

            this.socket.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /*
    发送登陆客户端通知
     */
    public void informServer(){
        try{
            OutputStream out = this.socket.getOutputStream();
            Message message = new Message(this.name, this.name, "run");
            out.write(message.toString().getBytes());
//            System.out.println(message); //test
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /*
    退出的时候发给Server
     */
    public void exitServer(){
        try{
            OutputStream out = this.socket.getOutputStream();
            Message message = new Message(this.name, this.name, "exit");
            out.write(message.toString().getBytes());
//            System.out.println(message); //test
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /*
    接收消息
     */
    public Message receiveMessage()   {
        try{
            // 从socket中拿到输入
            InputStream in = this.socket.getInputStream();
            BufferedReader buf = new BufferedReader(new InputStreamReader(in));

            // 还原信息
            String name = buf.readLine();        // 发送者的name
            String other_name = buf.readLine();  // 发送者的目标的name
            String message = buf.readLine();
            return new Message(name, other_name, message);
        } catch (Exception e){
            e.printStackTrace();
        }
        return new Message("wrong_sender", "wrong_receiver", "wrong_content");
    }

    /*
    发送消息
     */
    public void sendMessage(String other_name, String content){
        try{
            OutputStream out = this.socket.getOutputStream();
            Message message = new Message(this.name, other_name, content);
            out.write(message.toString().getBytes());
//            System.out.println(message);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("[1.1]请输入你的名字:");
        String name = scan.next();
        Client client = new Client(name);

        System.out.println("[1.2]请输入服务器ip地址,输入0使用默认(127.0.0.1):");
        String host = scan.next();
        if(!host.equals("0")){
            client.setHost(host);
        }

        System.out.println("[1.3]请输入服务器端口号,输入0表示默认(9999):");
        int port = scan.nextInt();
        if(port!=0){
            client.setPort(port);
        }

        // 连接, 并通知server
        client.connect();
//        client.informServer();

        // 开一个线程读取发送过来的
        new Thread(()->{
            while(true){
                synchronized (client.is_closed){
                    if(client.is_closed==true) break;
                    Message message = client.receiveMessage();
                    System.out.println("[reply]"+message.getSender_name()+":"+message.getContent());
                }
            }
        }).start();

        while(true){
            System.out.println("[2]请输入对方的名字(输入exit退出):");
            String other_name = scan.next().trim();

            if(other_name.toLowerCase().equals("exit")){
                System.out.println("bye");
//                client.exitServer();
                client.close();
                break;
            }

            System.out.println("[3]请输入你想说的话:");
            String trim = scan.nextLine(); //把换行读掉
            String sentence = scan.nextLine();


            client.sendMessage(other_name, sentence);
        }



    }

}
